# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=sphinx
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.4.5
pkgrel=1
pkgdesc="Python documentation generator (mingw-w64)"
arch=('any')
license=('LGPL-2.1')
url="http://sphinx.pocoo.org/"
makedepends=(
            "${MINGW_PACKAGE_PREFIX}-python3-colorama"
            "${MINGW_PACKAGE_PREFIX}-python3-docutils"
            "${MINGW_PACKAGE_PREFIX}-python3-imagesize"
            "${MINGW_PACKAGE_PREFIX}-python3-jinja"
            "${MINGW_PACKAGE_PREFIX}-python3-pygments"
            "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
            "${MINGW_PACKAGE_PREFIX}-python3-six"

            "${MINGW_PACKAGE_PREFIX}-python2-colorama"
            "${MINGW_PACKAGE_PREFIX}-python2-docutils"
            "${MINGW_PACKAGE_PREFIX}-python2-imagesize"
            "${MINGW_PACKAGE_PREFIX}-python2-jinja"
            "${MINGW_PACKAGE_PREFIX}-python2-pygments"
            "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
            "${MINGW_PACKAGE_PREFIX}-python2-six"
            )
checkdepends=(
              "${MINGW_PACKAGE_PREFIX}-python3-nose"
              "${MINGW_PACKAGE_PREFIX}-python2-nose"
              "${MINGW_PACKAGE_PREFIX}-python3-snowballstemmer"
              "${MINGW_PACKAGE_PREFIX}-python2-snowballstemmer"
              "${MINGW_PACKAGE_PREFIX}-python3-babel"
              "${MINGW_PACKAGE_PREFIX}-python2-babel"
              "${MINGW_PACKAGE_PREFIX}-python3-sphinx-alabaster-theme"
              "${MINGW_PACKAGE_PREFIX}-python2-sphinx-alabaster-theme"
              "${MINGW_PACKAGE_PREFIX}-python3-sphinx_rtd_theme"
              "${MINGW_PACKAGE_PREFIX}-python2-sphinx_rtd_theme"
              "${MINGW_PACKAGE_PREFIX}-python2-mock"
              "${MINGW_PACKAGE_PREFIX}-python3-mock"
              #'texlive-latexextra'
              )
source=("https://files.pythonhosted.org/packages/source/S/Sphinx/Sphinx-${pkgver}.tar.gz")
sha256sums=('c5df65d97a58365cbf4ea10212186a9a45d89c61ed2c071de6090cdf9ddb4028')

prepare() {
  # souce duplication is required because makefile modify source code
  # setyp.py --build tricks don't works well
  cp -a Sphinx-$pkgver python2-build
  cp -a Sphinx-$pkgver python3-build
  # change python2 interpreter
  find python2-build -type f -exec \
  sed -i '1s,^#! \?/usr/bin/\(env \|\)python$,#!/usr/bin/python2,' {} \;
  # change sphinx-binaries name in source code
  find python2-build -type f -name '*.py' -exec \
  sed -ri 's,(sphinx-(:?build|apidoc|autogen|quickstart)),\12,' {} \;
}

build() {
  cd "${srcdir}"
  for builddir in python{2,3}-build; do
    pushd ${builddir}
    ${MINGW_PREFIX}/bin/${builddir%-build} setup.py build
    popd
  done
}

package_python3-sphinx() {
  pkgdesc='Python3 documentation generator'
  depends=("${MINGW_PACKAGE_PREFIX}-python3-babel"
           "${MINGW_PACKAGE_PREFIX}-python3-colorama"
           "${MINGW_PACKAGE_PREFIX}-python3-docutils"
           "${MINGW_PACKAGE_PREFIX}-python3-imagesize"
           "${MINGW_PACKAGE_PREFIX}-python3-jinja"
           "${MINGW_PACKAGE_PREFIX}-python3-pygments"
           "${MINGW_PACKAGE_PREFIX}-python3-sphinx_rtd_theme"
           "${MINGW_PACKAGE_PREFIX}-python3-snowballstemmer"
           "${MINGW_PACKAGE_PREFIX}-python3-sphinx-alabaster-theme"
           "${MINGW_PACKAGE_PREFIX}-python3-six")
  #optdepends=("${MINGW_PACKAGE_PREFIX}-texlive-latexextra": for generation of PDF documentation')

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python3-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  #rm -f ${pkgdir}${MINGW_PREFIX}/bin/sphinx{.exe,-script.py,.exe.manifest}
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${_f}
  done
}

package_python2-sphinx() {
  pkgdesc='Python2 documentation generator'
  depends=("${MINGW_PACKAGE_PREFIX}-python2-babel"
           "${MINGW_PACKAGE_PREFIX}-python2-colorama"
           "${MINGW_PACKAGE_PREFIX}-python2-docutils"
           "${MINGW_PACKAGE_PREFIX}-python2-imagesize"
           "${MINGW_PACKAGE_PREFIX}-python2-jinja"
           "${MINGW_PACKAGE_PREFIX}-python2-pygments"
           "${MINGW_PACKAGE_PREFIX}-python2-sphinx_rtd_theme"
           "${MINGW_PACKAGE_PREFIX}-python2-snowballstemmer"
           "${MINGW_PACKAGE_PREFIX}-python2-sphinx-alabaster-theme"
           "${MINGW_PACKAGE_PREFIX}-python2-six")
  #optdepends=("${MINGW_PACKAGE_PREFIX}-texlive-latexextra": for generation of PDF documentation')

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python2-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${_f}
  done
}

package_mingw-w64-i686-python2-sphinx() {
  package_python2-sphinx
}

package_mingw-w64-i686-python3-sphinx() {
  package_python3-sphinx
}

package_mingw-w64-x86_64-python2-sphinx() {
  package_python2-sphinx
}

package_mingw-w64-x86_64-python3-sphinx() {
  package_python3-sphinx
}
